package bean;

public class Card implements Comparable<Card> {

	public static final int SPADE = 0;
	public static final int HEART = 1;
	public static final int DIAMOND = 2;
	public static final int CLUB = 3;
	public static final int NOTRUMP = 4;

	private static final String SUIT_INDEX = "SHDC";
	private static final String RANK_INDEX = "23456789TJQKA";

	public static boolean isValid(Card x) {
		if (x.getSuit() > 3 || x.getSuit() < 0)
			return false;
		if (x.getRank() > 12 || x.getRank() < 0)
			return false;
		return true;
	}

	public static Card getCard(String s) {
		Card result = new Card();
		result.setSuit(SUIT_INDEX.indexOf(s.charAt(0)));
		result.setRank(RANK_INDEX.indexOf(s.charAt(1)));
		return result;
	}

	private int rank;
	private int suit;

	public Card() {

	}

	public Card(String s) {
		Card tmp = Card.getCard(s);
		this.rank = tmp.getRank();
		this.suit = tmp.getSuit();
	}

	public int getRank() {
		return rank;
	}

	public void setRank(int rank) {
		this.rank = rank;
	}

	public int getSuit() {
		return suit;
	}

	public void setSuit(int suit) {
		this.suit = suit;
	}

	@Override
	public int compareTo(Card o) {
		int thisId = (4 - this.suit) * 13 + this.rank;
		int targetId = (4 - o.suit) * 13 + o.rank;
		if (thisId == targetId)
			return 0;
		return thisId > targetId ? 1 : -1;
	}

	public String toString() {
		return SUIT_INDEX.charAt(this.getSuit()) + "" + RANK_INDEX.charAt(this.getRank());
	}

	@Override
	public boolean equals(Object o) {
		if (o instanceof Card) {
			Card x = (Card) o;
			return suit == x.getSuit() && rank == x.getRank();
		}
		return false;
	}
	
	public char getCardEncode(){
		return (char)((suit) * 13 + this.rank+48);
	}

}
